package com.nju.crawler.service;

import com.nju.crawler.repository.PersistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询编程语言、开源许可证的占比情况
 *
 * @authorliyunfei
 * @date2022/11/20
 **/
@Service
public class QueryProProportionService {
    @Autowired
    PersistService persistService;

    /**
     * 查询编程语言和开源许可证的情况
     * @return
     */
    public Map<String, Map> queryProportion(){
        Map<String,Map> rs = new HashMap<>(2);
        final int limit = 10;

        List<Map<String, Object>> languageMap = persistService.languageListQuery(limit);
        List<Map<String, Object>> licenseMap = persistService.licenseListQuery(limit);//FIXME license 未严格遵循 10条数据？
        List<String> langNameList = new ArrayList<>(limit);
        List<Integer> langValueList = new ArrayList<>(limit);
        List<String> licenseNameList = new ArrayList<>(limit);
        List<Integer> licenseValueList = new ArrayList<>(limit);

        final String languageKeyName = "language";
        final String licenseKeyName = "license";
        final String cntKeyName = "cnt";

        for(Map<String,Object> map:languageMap){
            langNameList.add((String) map.get(languageKeyName));
            langValueList.add((Integer) map.get(cntKeyName));
        }
        for(Map<String,Object> map:licenseMap){
            licenseNameList.add((String) map.get(licenseKeyName));
            licenseValueList.add((Integer) map.get(cntKeyName));
        }

        Map<String,List> langMap = new HashMap<>(2);
        Map<String,List> licMap = new HashMap<>(2);
        langMap.put("langNameList",langNameList);
        langMap.put("langValueList",langValueList);
        licMap.put("licenseNameList",licenseNameList);
        licMap.put("licenseValueList",licenseValueList);
        rs.put("langMap",langMap);
        rs.put("licenseMap",licMap);

        return rs;
    }
}
